<template>
  <div class="home-wrap">
    <HomeHeader></HomeHeader>
    <HomeSwiper :list="swiperList"></HomeSwiper>
    <HomeIcons :list="iconList"></HomeIcons>
    <HomeRecommend :list="recommendList"></HomeRecommend>
    <home-weekend :list="weekendList"></home-weekend>
  </div>
</template>

<script>
// 导入Home页相应的子组件
import HomeHeader from "./components/Header";
import HomeSwiper from "./components/Swiper";
import HomeIcons from "./components/Icons";
import HomeRecommend from "./components/Recommend";
import HomeWeekend from "./components/Weekend";
//导入ajax模块
import axios from "axios";

import { mapState } from "vuex";
export default {
  name: "Home",
  data() {
    return {
      iconList: [],
      recommendList: [],
      swiperList: [],
      weekendList: [],
      lastCity: ""
    };
  },
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  computed: {
    ...mapState(["city"])
  },
  methods: {
    getHomeInfo() {
      //这里设置了跨域请求在webpack里面设置
      axios.get("/api/index.json").then(this.getHomeInfoSucc);
    },
    getHomeInfoSucc(res) {
      const resData = res.data;
      if (resData.ret && resData.data) {
        this.iconList = resData.data.iconList;
        this.recommendList = resData.data.recommendList;
        this.swiperList = resData.data.swiperList;
        this.weekendList = resData.data.weekendList;
      }
    }
  },
  mounted() {
    this.lastCity = this.city;
    this.getHomeInfo();
  },
  activated() {
    if( this.lastCity != this.city )
    {
      this.getHomeInfo()
      this.lastCity = this.city
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang='stylus'>
.home-wrap {
  touch-action: none;
}
</style>
